package cn.net.isir.controller.miniprogram;

import cn.net.isir.common.CacheObjectManager;
import cn.net.isir.common.RequestObject;
import cn.net.isir.common.ResultObject;
import cn.net.isir.common.TokenManager;
import cn.net.isir.entity.BankCard;
import cn.net.isir.entity.User;
import cn.net.isir.entity.UserInfo;
import cn.net.isir.service.BankCardService;
import cn.net.isir.service.UserInfoService;
import cn.net.isir.service.UserService;
import cn.net.isir.utils.AppRequestUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuxubo
 * @filename MinipMy.java
 * @package cn.net.isir.controller.miniprogram
 * @email lxb@isir.net.cn
 * @date 2019/03/01
 * @comment
 * @since 1.8
 */
@RestController
@RequestMapping("/minip/my")
public class MinipMyController {
    @Resource
    private UserService userService;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private BankCardService bankCardService;

    /**
     * 个人信息
     *
     * @param obj 带有key的参数
     * @return 返回用户信息
     */
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public ResultObject bindBankcard(RequestObject obj) {
        //声明参数解码后的储存变量
        Map<String, Object> params = new HashMap<>();
        //需要检查的字段名
        List<String> columns = new ArrayList<>();
        columns.add("key");
        //声明返回值
        ResultObject ro = AppRequestUtils.parametersCheck(obj, params, columns);
        //检查失败则返回缺失的字段提示
        if (ro.getResult() != 200) {
            return ro;
        }
        String key = String.valueOf(params.get("key"));
        // 从缓存中,根据key查询获取到的userId
        String userId = CacheObjectManager.minipCRMKey.get(key);
        if (userId == null) {
            return new ResultObject(502, "need login.", null);
        }
        UserInfo userInfo = userInfoService.selectByUserId(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("avatar", userInfo.getAvatar());
        map.put("name", userInfo.getName());
        map.put("mobile", userInfo.getTel());
        return new ResultObject(200, "success", map);
    }

    /**
     * 修改头像
     *
     * @param obj 带有key的参数
     * @return 修改结果
     */
    @RequestMapping(value = "/changeavatar", method = RequestMethod.PUT)
    public ResultObject changeavatar(@RequestBody RequestObject obj) {
        //声明参数解码后的储存变量
        Map<String, Object> params = new HashMap<>();
        //需要检查的字段名
        List<String> columns = new ArrayList<>();
        columns.add("key");
        columns.add("fileId");
        //声明返回值
        ResultObject ro = AppRequestUtils.parametersCheck(obj, params, columns);
        //检查失败则返回缺失的字段提示
        if (ro.getResult() != 200) {
            return ro;
        }
        String key = String.valueOf(params.get("key"));
        String fileId = String.valueOf(params.get("fileId"));
        // 从缓存中,根据key查询获取到的userId
        String userId = CacheObjectManager.minipCRMKey.get(key);
        if (userId == null) {
            return new ResultObject(502, "need login.", null);
        }
        UserInfo userInfo = userInfoService.selectByUserId(userId);
        userInfo.setAvatar(fileId);
        int result = userInfoService.update(userInfo);
        return new ResultObject(200, "success", result);
    }

    /**
     * 修改手机号
     *
     * @param obj 带有key的参数
     * @return 修改结果
     */
    @RequestMapping(value = "/changemobile", method = RequestMethod.PUT)
    public ResultObject changemobile(@RequestBody RequestObject obj) {
        //声明参数解码后的储存变量
        Map<String, Object> params = new HashMap<>();
        //需要检查的字段名
        List<String> columns = new ArrayList<>();
        columns.add("key");
        columns.add("mobile");
        //声明返回值
        ResultObject ro = AppRequestUtils.parametersCheck(obj, params, columns);
        //检查失败则返回缺失的字段提示
        if (ro.getResult() != 200) {
            return ro;
        }
        String key = String.valueOf(params.get("key"));
        String mobile = String.valueOf(params.get("mobile"));
        // 从缓存中,根据key查询获取到的userId
        String userId = CacheObjectManager.minipCRMKey.get(key);
        if (userId == null) {
            return new ResultObject(502, "need login.", null);
        }
        UserInfo userInfo = userInfoService.selectByUserId(userId);
        userInfo.setTel(mobile);
        int result = userInfoService.update(userInfo);
        return new ResultObject(200, "success", result);
    }

    /**
     * 修改密码
     *
     * @param obj 带有key的参数
     * @return 修改结果
     */
    @RequestMapping(value = "/changepwd", method = RequestMethod.PUT)
    public ResultObject changepwd(@RequestBody RequestObject obj) {
        //声明参数解码后的储存变量
        Map<String, Object> params = new HashMap<>();
        //需要检查的字段名
        List<String> columns = new ArrayList<>();
        columns.add("key");
        columns.add("oldPwd");
        columns.add("newPwd");
        //声明返回值
        ResultObject ro = AppRequestUtils.parametersCheck(obj, params, columns);
        //检查失败则返回缺失的字段提示
        if (ro.getResult() != 200) {
            return ro;
        }
        String key = String.valueOf(params.get("key"));
        // 从缓存中,根据key查询获取到的userId
        String userId = CacheObjectManager.minipCRMKey.get(key);
        if (userId == null) {
            return new ResultObject(502, "need login.", null);
        }
        String oldPwd = String.valueOf(params.get("oldPwd"));
        String newPwd = String.valueOf(params.get("newPwd"));
        User user = userService.selectByPrimaryKey(userId);
        if (!TokenManager.checkPwd(user.getUsername(), oldPwd)) {
            return new ResultObject(501, "old password is not match", 0);
        }
        user.setPassword(newPwd);
        TokenManager.editUser(user, null);
        return new ResultObject(200, "success", null);
    }

    /**
     * 修改银行卡信息
     *
     * @param obj 带有key的参数
     * @return 修改结果
     */
    @RequestMapping(value = "/changebankcard", method = RequestMethod.PUT)
    public ResultObject changebankcard(@RequestBody RequestObject obj) {
        //声明参数解码后的储存变量
        Map<String, Object> params = new HashMap<>();
        //需要检查的字段名
        List<String> columns = new ArrayList<>();
        columns.add("key");
        columns.add("name");
        columns.add("bankCardNo");
        columns.add("belongTo");
        columns.add("openingBank");
        //声明返回值
        ResultObject ro = AppRequestUtils.parametersCheck(obj, params, columns);
        //检查失败则返回缺失的字段提示
        if (ro.getResult() != 200) {
            return ro;
        }
        String key = String.valueOf(params.get("key"));
        // 从缓存中,根据key查询获取到的userId
        String userId = CacheObjectManager.minipCRMKey.get(key);
        if (userId == null) {
            return new ResultObject(502, "need login.", null);
        }
        String name = String.valueOf(params.get("name"));
        String bankCardNo = String.valueOf(params.get("bankCardNo"));
        String belongTo = String.valueOf(params.get("belongTo"));
        String openingBank = String.valueOf(params.get("openingBank"));
        BankCard bankCard = new BankCard();
        bankCard.setUserId(userId);
        bankCard = bankCardService.selectOne(bankCard);
        if (bankCard == null) {
            return new ResultObject(500, "bankcard info is not found.", null);
        }
        bankCard.setName(name);
        bankCard.setCardNo(bankCardNo);
        bankCard.setBelongTo(belongTo);
        bankCard.setOpeningBank(openingBank);
        int result = bankCardService.update(bankCard);
        return new ResultObject(200, "success", result);
    }

    /**
     * 获取银行卡信息
     *
     * @param obj 带有key的参数
     * @return 修改结果
     */
    @RequestMapping(value = "/getbankcard", method = RequestMethod.GET)
    public ResultObject getbankcard(RequestObject obj) {
        //声明参数解码后的储存变量
        Map<String, Object> params = new HashMap<>();
        //需要检查的字段名
        List<String> columns = new ArrayList<>();
        columns.add("key");
        //声明返回值
        ResultObject ro = AppRequestUtils.parametersCheck(obj, params, columns);
        //检查失败则返回缺失的字段提示
        if (ro.getResult() != 200) {
            return ro;
        }
        String key = String.valueOf(params.get("key"));
        // 从缓存中,根据key查询获取到的userId
        String userId = CacheObjectManager.minipCRMKey.get(key);
        if (userId == null) {
            return new ResultObject(502, "need login.", null);
        }
        BankCard bankCard = new BankCard();
        bankCard.setUserId(userId);
        bankCard = bankCardService.selectOne(bankCard);
        if (bankCard == null) {
            return new ResultObject(500, "bankcard info is not found.", null);
        }
        return new ResultObject(200, "success", bankCard);
    }

}
